from django.forms.models import model_to_dict


# 图片模型转字典
def utils_model_to_dict(model, *args, **kwargs):
    data = model_to_dict(model)
    
    # 处理外键字段
    if hasattr(model, 'account') and model.account:
        data['account'] = {
            'aid': str(model.account.aid),
        }
    
    # 处理文件字段，添加文件URL
    if hasattr(model, 'image') and model.image:
        data['image_url'] = model.image.url
        # 移除原始的image字段，避免FieldFile对象无法JSON序列化的问题
        if 'image' in data:
            del data['image']
    
    return data